package com.qianfeng.controller;

import com.qianfeng.entry.YQAddEntry;
import com.qianfeng.entry.YQComEntry;
import com.qianfeng.entry.YQMapEntry;
import com.qianfeng.service.impl.YQServiceImpl;
import com.qianfeng.util.ResMsg;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 疫情业务的控制器
 */
@RestController
@RequestMapping(value = "/v1")
public class YQController {

    @Autowired
    YQServiceImpl yqService;

    /**
     * 全国疫情分布地图
     * @return
     */
    @ApiOperation(value = "地图数据",notes = "全国地图数据")
    @RequestMapping(value = "/map/{startDate}/{endDate}",method = RequestMethod.GET)
    public ResMsg mapyqs(@PathVariable(value = "startDate") String startDate,@PathVariable(value = "endDate") String endDate){
        List<Map<String,String>> res1 = null;
        System.out.println("我是许煌泽，哈哈哈哈哈哈哈哈哈哈哈哈哈");
        System.out.println("helloworld");
        ResMsg msg = null;
        try {
            List<YQMapEntry> los = yqService.mapYiQings(startDate,endDate);
            res1 = initMap();
            for (Map<String,String> m:res1) {
                for (YQMapEntry om:los) {
                    Map<String, String> m1 = new HashMap<>();
                    String pro = om.getProvince();
                    if (pro.contains("省")) {
                        pro = om.getProvince().replace("省", "");
                    } else if (pro.contains("市")) {
                        pro = om.getProvince().replace("市", "");
                    } else if (pro.contains("内蒙古")) {
                        pro = om.getProvince().substring(0, 3);
                    } else if (pro.contains("自治区")) {
                        pro = om.getProvince().substring(0, 2);
                    } else if (pro.contains("特别行政区")) {
                        pro = om.getProvince().substring(0, 2);
                    }
                    //判断
                    if(pro.equals(m.get("name"))){
                        m.put("value",om.getConfirm_cnt()+"");
                        break;
                    }
                }

            }
            //成功后
            msg = ResMsg.success();
            msg.setData(res1);
        } catch (NumberFormatException e) {
            msg = ResMsg.fail();
            msg.setData("[]");
            e.printStackTrace();
        }
        return msg;
    }


    /**
     * 新增确诊和疑似
     * @return
     */
    @ApiOperation(value = "add and possible",notes = "对比1")
    @RequestMapping(value = "/add/{interval}",method = RequestMethod.GET)
    public ResMsg addyqs(@PathVariable(value = "interval") int interval){
        List<String> dts = null;
        List<String> confirms = null;
        List<String> possibles = null;
        Map<String,List<String>> map = new HashMap<>();
        ResMsg msg = null;
        try {
            //获取数据
            List<YQAddEntry> los = yqService.addYiQings(interval);
            if (los.size() > 0) {
                dts = new ArrayList<>();
                confirms = new ArrayList<>();
                possibles = new ArrayList<>();
                for (YQAddEntry lo:los){
                    String[] ddts = lo.getDt().split("\\-");
                    dts.add(ddts[1] +"/"+ddts[2]);
                    confirms.add(lo.getConfirms()+"");
                    possibles.add(lo.getPossibels()+"");
                }
            }
            //将数据添加到数据msg中
            msg = ResMsg.success();
            map.put("dts",dts);
            map.put("confirms",confirms);
            map.put("possibles",possibles);
            msg.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            msg = ResMsg.fail();
            msg.setData(map);
        }
        return msg;
    }

    /**
     * 全国和湖北的新增确诊对比
     * @return
     */
    @ApiOperation(value = "all and hb",notes = "对比2")
    @RequestMapping(value = "/com/{interval}",method = RequestMethod.GET)
    public ResMsg comyqs(@PathVariable(value = "interval") int interval){
        List<String> dts = null;
        List<String> all_confirms = null;
        List<String> hubei_confirms = null;
        Map<String,List<String>> map = new HashMap<>();
        ResMsg msg = null;
        try {
            //获取数据
            List<YQComEntry> los = yqService.addComYiQings(interval);
            if (los.size() > 0) {
                dts = new ArrayList<>();
                all_confirms = new ArrayList<>();
                hubei_confirms = new ArrayList<>();
                for (YQComEntry lo:los){
                    String[] ddts = lo.getDt().split("\\-");
                    dts.add(ddts[1] +"/"+ddts[2]);
                    all_confirms.add(lo.getAll_confirms()+"");
                    hubei_confirms.add(lo.getConfirms()+"");
                }
            }
            //将数据添加到数据msg中
            msg = ResMsg.success();
            map.put("dts",dts);
            map.put("all_confirms",all_confirms);
            map.put("hubei_confirms",hubei_confirms);
            msg.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            msg = ResMsg.fail();
            msg.setData(map);
        }
        return msg;
    }

    /**
     * 初始化map
     * @return
     */
    public static List<Map<String,String>> initMap(){
        List<Map<String,String>> maps = new ArrayList<>();
        String[] arr = {"北京","天津","上海","重庆","河北","山西","辽宁","吉林","黑龙江",
                "江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东",
                "海南","四川","贵州","云南","陕西","甘肃","青海","台湾","内蒙古","广西",
                "西藏","宁夏","新疆","香港","澳门"};
        for (int i=0;i < 34;i++){
            Map<String,String> map = new HashMap<>();
            map.put("name",arr[i]);
            map.put("value","0");
            maps.add(map);
        }
        return maps;
    }

}
